/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	ATenantUserConverter.java
 * Created：		2016年7月30日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.user.converter;

import java.util.ArrayList;

import org.springframework.core.convert.converter.Converter;

import com.thor.commons.entity.KV;
import com.thor.commons.entity.UCN;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.rest.user.ATenantUser;
import com.thor.palmmall.spi.user.STenantUser;

/**
 * @author wolv
 *
 */
public class ATenantUserConverter implements Converter<STenantUser, ATenantUser> {

  public static ATenantUserConverter getInstance() {
    if (instance == null)
      instance = new ATenantUserConverter();
    return instance;
  }

  private static ATenantUserConverter instance;

  private ATenantUserConverter() {
  }

  @Override
  public ATenantUser convert(STenantUser source) {
    if (source == null)
      return null;

    ATenantUser target = new ATenantUser();
    target.setCode(source.getCode());
    target.setName(source.getName());
    target.setPermissions(new ArrayList<String>());
    target.getPermissions().addAll(source.getPermissions());
    target.setStores(ConverterUtil.convert(source.getStores(), AStoreConverter.getInstance()));
    target.setUuid(source.getUuid());
    target.setTenant(UCN.newInstance(source.getTenant()));
    target.setTelephone(source.getTelephone());
    target.setAssistantType(source.getAssistantType());
    target.setCompanies(
        ConverterUtil.convert(source.getCompanies(), ACompanyConverter.getInstance()));
    target.setPermObjects(
        ConverterUtil.convert(source.getPermObjects(), APermObjectsConverter.getInstance()));

    target.setUserGroups(new ArrayList<KV>());
    if (source.getUserGroups() != null)
      for (KV sourceUserGroup : source.getUserGroups())
        target.getUserGroups().add(KV.newInstance(sourceUserGroup));

    return target;
  }

}
